import 'bootstrap/dist/css/bootstrap.min.css';
require('../css/purchase/shop.less');
require('../css/purchase/index.less');
require('../css/purchase/common.less');

var $ = require('jquery');
import 'bootstrap/dist/js/bootstrap.min.js';
import 'bootstrap-paginator/bootstrap-paginator.js';

let headerTemplate = require('../view/templateWidget/header.hbs');
let footerTemplate = require('../view/templateWidget/footer.hbs');
let infoContTemplate = require('../view/templateWidget/infoCont.hbs');

$('header').html(headerTemplate);
$('footer').html(footerTemplate);
require('../helper/header.js');

// getClassifyShow();

var delive_area = "";
var pubTime = "";
var purchType = "";
var searchWords ="";
// function getClassifyShow() {
    var parUrl = window.location.href;
    console.log(parUrl);
    var reg = new RegExp("(^|&)" +'keywords' + "=([^&]*)(&|$)"); 
    var resKeyWord = decodeURI(parUrl.match(reg)[2]);
    $("#classifyName").text(resKeyWord);
    $("#txtSearch").val(resKeyWord);
    searchWords = resKeyWord;
    console.log(searchWords);
    searchResult();
// }
$("#txtSearch").on("change",function(){
    $("#topSearch").attr("target",""); 
    var newSearch = $("#txtSearch").val();
    $("#classifyName").text(newSearch);
    searchWords = newSearch;
    searchResult();
})
// 交货地
$("#showSelect").on('click', function (e) {
    $("#selectCity").show();
    $.ajax({
        type: "get",
        url: "http://purchasing.mhw001.com/getDistrictList",
        dataType: "json",
        success: function (result) {
            var li1 = '';
            for (var i = 0; i < result.data.length; i++) {
                li1 += '<li class = "jhd_province" id = "' + result.data[i].district_id + '">' + result.data[i].district_name +
                    '<ul class = "citys_list"></ul>' +
                    '</li>';
            }
            $('#province_list').empty().append(li1);
        }
    });
    $("#selectCity").delegate('.jhd_province', 'click', function (e) {
        var proId = $(this).attr("id");
        $('.jhd_province').removeClass('selectedPro');
        $(this).addClass('selectedPro');
        var obj = $(this);
        $.ajax({
            type: "get",
            url: "http://purchasing.mhw001.com/getDistrictList",
            dataType: "json",
            data: {
                'district_id': proId
            },
            success: function (res) {
                var li2 = '';
                for (var i = 0; i < res.data.length; i++) {
                    li2 += '<li class = "jhd_city" id="' + res.data[i].district_id + '">' + res.data[i].district_name + '</li>';
                }
                $(obj).find('.citys_list').empty().append(li2);
                $('.citys_list').hide();
                $(obj).find('.citys_list').show();
                var citys = $(obj).find('.citys_list');
                $(citys).delegate('.jhd_city', 'click', function (e) {
                    e.stopPropagation();
                    var cityName = $(this).text();
                    $('#select_area').text(cityName);
                    delive_area = cityName;
                    console.log(delive_area);
                    searchResult();
                    $('.jhd_province').removeClass('selectedPro');
                    $(citys).hide();
                    $("#selectCity").hide();
                });
            }
        })
    })
})
// 发布时间点击变换排序
$("#down_time").on('click', function () {
    if ($(this).find('img').attr('src') == '/img/purchase/down.png') {
        $(this).find('img').attr('src', '/img/purchase/up.png');
        // 处理数据
        pubTime = "asc";
    } else {
        $(this).find('img').attr('src', '/img/purchase/down.png');
        // 处理数据  
        pubTime = "";
    }
    console.log(pubTime);
    searchResult();
})
// 采购类型
$("#purchType").on("change", function () {
    var selected_type = $("#purchType").val();
    // console.log(selected_type);
    if (selected_type == "spotgoods") {
        purchType = 0;
    } else if (selected_type == "processgoods") {
        purchType = 1;
    } else {
        purchType = "";
    }
    console.log(purchType);
    searchResult();
})

function searchResult(){
    $.ajax({
        type: "get",
        url: "http://purchasing.mhw001.com/searchPurchasing",
        dataType: "json",
        data: {
            'delivery': delive_area,
            'order_by': pubTime,
            'purchase_type': purchType,
            'search_words':searchWords
        },
        success: function (result) {
            console.log(result.data);
            if(result.data.data.length=="0"){
                $('#infoCont').empty();
                $(".info_display").hide();
                $('.noMoreData').show();
                $('#example').hide();
                return false;
            }else if(result.data.last_page=="1"){
                $('#infoCont').html(infoContTemplate({
                    data: result.data.data
                }));
                $(".info_display").show();
                $('.noMoreData').hide();
                $('#example').hide();
            }else{
                $('#infoCont').html(infoContTemplate({
                    data: result.data.data
                }));
                $(".info_display").show();
                $('.noMoreData').hide();
                $('#example').show();
            }
            // 分页
            var allPages = result.data.last_page;
            var currentPage = result.data.current_page;
            $('#pageLimit').bootstrapPaginator({
                currentPage: currentPage,
                totalPages: allPages,
                size: "normal",
                bootstrapMajorVersion: 3,
                alignment: "right",
                numberOfPages: 8,
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    } //默认显示的是第一页。
                },
                onPageClicked: function (event, originalEvent, type, page) { //给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
                    $.ajax({
                        url: 'http://purchasing.mhw001.com/searchPurchasing',
                        type: 'get',
                        data: {
                            'page': page
                        },
                        dataType: 'JSON',
                        success: function (callback) {
                            // console.log(callback);
                            $('#infoCont').empty();
                            $('#infoCont').html(infoContTemplate({
                                data: callback.data.data
                            }));
                            var page_count = callback.page_count;
                            var page_cont = callback.page_content;
                            $('#infoCont').append(page_cont);
                            $('#last_page').text(page_count);
                        }
                    })
                }
            });
        }
    })
}